154A - Hometask - CodeForces Solution


greedy *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define fast_cin() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
// long long int n,m,k,flag=0,sz=0,v[51][51],dx[]={0,0,0,1},dy[]={1,0,0,0};
// string s[51];
// vector<pair<long long int,pair<long long int,long long int>>>vv;
// void dfschng(long long int x,long long int y)
// {
//  if(x<0 || y<0 || x>=n || y>=m)
//  {
//   return;
//  }
//  if(v[x][y] || s[x][y]=='*')
//  {
//   return;
//  }
//  //cout<<x<<" "<<y<<"\n";
//  v[x][y]=1;s[x][y]='*';
//  for(long long int i=0;i<4;i++)
//  {
//   long long int xx=x+dx[i],yy=y+dy[i];
//   dfschng(xx,yy);
//  }
// }
// void dfs(long long int x,long long int y)
// {
//  if(x<0 || y<0 || x>=n || y>=m)
//  {
//   return;
//  }
//  if(v[x][y] || s[x][y]=='*')
//  {
//   return;
//  }
//  v[x][y]=1;sz++;
//  if(x==0 || y==0 || x==(n0) || y==(m0))
//  {
//   flag=1;
//  }
//  for(long long int i=0;i<4;i++)
//  {
//   long long int xx=x+dx[i],yy=y+dy[i];
//   dfs(xx,yy);
//  }
// }
// long long int n;
//  cin>>n;
//  for(long long int i=1;i<=n;i+=2)
//  {
//   cout<<i<<" ";
//  }
//  for(long long int i=n-(n&1);i>=1;i-=2)
//  {
//   cout<<i<<" ";
//  }
//  cout<<"\n";
void solve()
{
 long long int n,k,a=0,b=0,op=0;
 string s;
 cin>>s;
 n=s.size();
 cin>>k;
 string fp[k];
 for(auto&s:fp)
 {
  cin>>s;
 }
 for(long long int i=0;i<k;i++)
 {
  for(long long int j=0;j<n;j++)
  {
   a=0;b=0;
   while(s[j]==fp[i][0] || s[j]==fp[i][1] && j<n)
   {
    a+=(s[j]==fp[i][0]);
    b+=1-(s[j]==fp[i][0]);j++;
   }
   op+=a*(a<b)+b*(1-(a<b));
  }
 }
 cout<<op;
}
int main()
{
    fast_cin();
    long long int t;
    //cin>>t;
    t=1;
    while(t--)
    {
     solve();
    }
   return 0;
}


Comments

Submit
0 Comments
More Questions

1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer